import { ApiProperty } from '@nestjs/swagger';
import { IsEnum, IsNotEmpty, Length, ValidateIf } from 'class-validator';
import { IsMobilePhone } from 'src/utils/Validator';

export class SigninUserDto {
  @ApiProperty({
    type: String,
    example: '13800138000',
    description: '手机号',
    required: false,
  })
  @ValidateIf((o) => o.phone)
  @IsNotEmpty({
    message: '手机号不能为空',
  })
  @IsMobilePhone({
    message: '手机号码格式不正确',
  })
  phone: string;

  @ApiProperty({
    type: String,
    example: '666666',
    description: '验证码',
    required: false,
  })
  @ValidateIf((o) => o.code)
  @IsNotEmpty({
    message: '验证码不能为空',
  })
  @Length(6, 6, {
    message: '验证码必须是6位字符',
  })
  code: string;

  @ApiProperty({
    type: String,
    example: 'zhangsan',
    description: '用户名',
    required: false,
  })
  @IsNotEmpty({
    message: '账号参数不能为空',
  })
  @Length(6, 64, {
    message: '账号长度范围6-64位',
  })
  @ValidateIf((o) => o.username)
  username: string;

  @ApiProperty({
    type: String,
    example: '1234qwer',
    description: '密码',
    required: false,
  })
  @IsNotEmpty({
    message: '密码参数不能为空',
  })
  @Length(6, 18, {
    message: '密码长度范围6-18位',
  })
  @ValidateIf((o) => o.password)
  password: string;

  @ApiProperty({
    enum: ['sms', 'password'],
    description:
      'sms:短信登录 password:密码登录 sms=>phone|code password=>username|password|code',
    example: 'sms',
  })
  @IsNotEmpty({
    message: '类型不能为空',
  })
  @IsEnum(['sms', 'password'], {
    message: 'type类型必须是sms或password',
  })
  type: string;
}
